Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Imports Vintasoft.Pdf.Drawing
Imports Vintasoft.Pdf.Tree
Imports Vintasoft.Pdf.Tree.Fonts
Imports Vintasoft.Pdf.Visualization
Imports Vintasoft.Pdf.Drawing.GraphicsFigures

Imports DemosCommonCode.Imaging


Namespace Pdf
	Public Partial Class PdfContentEditorToolStrip
		Inherits ToolStrip

		#Region "Fields"

		Private _currentFigureButton As ToolStripButton
		Private _addText As String = ""
		Private _pen As PdfPen
		Private _brush As PdfBrush
		Private _selectHighlightPenDialogModeIndex As Integer = 0

		#End Region



		#Region "Constructors"

		Public Sub New()
			MyBase.New()
			InitializeComponent()
			Enabled = False
		End Sub

		Public Sub New(container As IContainer)
			MyBase.New()
			container.Add(Me)

			InitializeComponent()
			Enabled = False
		End Sub

		#End Region



		#Region "Properties"

		Private _editorTool As PdfEditorTool
		Public Property EditorTool() As PdfEditorTool
			Get
				Return _editorTool
			End Get
			Set
				If _editorTool IsNot Nothing Then
					RemoveHandler _editorTool.FigureBuildingFinished, New EventHandler(AddressOf _editorTool_FinishBuildFigure)
					RemoveHandler _editorTool.FigureBuildingStarted, New EventHandler(AddressOf _editorTool_BeginBuildFigure)
				End If
				_editorTool = value
				If _editorTool IsNot Nothing Then
					AddHandler _editorTool.FigureBuildingFinished, New EventHandler(AddressOf _editorTool_FinishBuildFigure)
					AddHandler _editorTool.FigureBuildingStarted, New EventHandler(AddressOf _editorTool_BeginBuildFigure)
				End If
				Enabled = _editorTool IsNot Nothing
			End Set
		End Property

		Private _viewerTool As PdfViewerTool
		Public Property ViewerTool() As PdfViewerTool
			Get
				Return _viewerTool
			End Get
			Set
				_viewerTool = value
			End Set
		End Property

		''' <summary>
		''' Gets the PdfPage associated with a imageViewer.Image.
		''' </summary>
		Friend ReadOnly Property FocusedPage() As PdfPage
			Get
				Return _editorTool.CurrentPage
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Sub ShowMessage_CurrentImageIsNotPdfPage()
			MessageBox.Show("Current image is not PDF page. Save document and try again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
		End Sub

		Private Function SetPenSettings() As Boolean
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return False
			End If
			If _pen Is Nothing Then
				_pen = New PdfPen(Color.Black)
				_pen.LineJoinStyle = GraphicsStateLineJoinStyle.RoundJoin
			End If
			Dim form As New PropertyGridForm(_pen, "Set properties of pen", True)
			Return form.ShowDialog() = DialogResult.OK
		End Function

		Private Function SetHighlightPenSettings() As Boolean
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return False
			End If
			If _pen Is Nothing Then
				_pen = New PdfPen(Color.Black, 10)
				_pen.LineJoinStyle = GraphicsStateLineJoinStyle.RoundJoin
			End If
			Dim dlg As New SelectHighlightForm(_pen)
			dlg.ModeIndex = _selectHighlightPenDialogModeIndex
			If dlg.ShowDialog() = DialogResult.OK Then
				_selectHighlightPenDialogModeIndex = dlg.ModeIndex
				Return True
			End If
			Return False
		End Function

		Private Function SetHighlightBrushSettings() As Boolean
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return False
			End If
			If _brush Is Nothing Then
				_brush = New PdfBrush(Color.Black)
			End If
			Dim dlg As New SelectHighlightForm(_brush)
			dlg.ModeIndex = _selectHighlightPenDialogModeIndex
			If dlg.ShowDialog() = DialogResult.OK Then
				_selectHighlightPenDialogModeIndex = dlg.ModeIndex
				Return True
			End If
			Return False
		End Function

		Private Function SetBrushSettings() As Boolean
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return False
			End If
			If _brush Is Nothing Then
				_brush = New PdfBrush(Color.FromArgb(70, Color.FromArgb(255, 255, 0)))
			End If
			Dim form As New PropertyGridForm(_brush, "Set properties of brush", True)
			Return form.ShowDialog() = DialogResult.OK
		End Function


		Private Sub addTextToolStripButton_Click(sender As Object, e As System.EventArgs)
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return
			End If
			StartAction("Search fonts")
			Dim fonts As IList(Of PdfFont) = FocusedPage.Document.Fonts
			EndAction()
			Dim addTextDialog As New AddTextForm(FocusedPage.Document, fonts, _addText)
			If addTextDialog.ShowDialog() = DialogResult.OK Then
				_addText = addTextDialog.TextToAdd
				Dim textBrush As New PdfBrush(addTextDialog.TextColor)
				_editorTool.StartBuildTextLine(_addText, addTextDialog.TextFont, addTextDialog.TextSize, textBrush)
				_currentFigureButton = addTextToolStripButton
			End If
		End Sub


		Private Sub addTextBoxToolStripButton_Click(sender As Object, e As System.EventArgs)
			If FocusedPage Is Nothing Then
				ShowMessage_CurrentImageIsNotPdfPage()
				Return
			End If
			StartAction("Search fonts")
			Dim fonts As IList(Of PdfFont) = FocusedPage.Document.Fonts
			EndAction()
			Dim addTextDialog As New AddTextForm(FocusedPage.Document, fonts, _addText)
			If addTextDialog.ShowDialog() = DialogResult.OK Then
				Dim pen As PdfPen = Nothing
				If SetPenSettings() Then
					pen = _pen
				End If
				Dim brush As PdfBrush = Nothing
				If SetBrushSettings() Then
					brush = _brush
				End If
				_addText = addTextDialog.TextToAdd
				Dim textBox As New TextBoxFigure(New PdfBrush(addTextDialog.TextColor), _addText, addTextDialog.TextFont, addTextDialog.TextSize)
				textBox.Pen = pen
				textBox.Brush = brush
				Dim form As New PropertyGridForm(textBox, "Set properties of TextBoxFigure", True)
				If form.ShowDialog() = DialogResult.OK Then
					_editorTool.StartBuildFigure(textBox, PdfGraphicsFigureBuildStyle.DownMoveUp)
					_currentFigureButton = addTextBoxToolStripButton
				End If
			End If
		End Sub


		Private Sub drawImageToolStripButton_Click(sender As Object, e As System.EventArgs)
			If FocusedPage Is Nothing Then
				drawImageToolStripButton.Checked = False
				ShowMessage_CurrentImageIsNotPdfPage()
				Return
			End If
			Dim selectResourceDialog As New SelectResourceForm(FocusedPage.Document)
			If selectResourceDialog.ShowDialog() = DialogResult.OK Then
				_currentFigureButton = drawImageToolStripButton
				_editorTool.StartBuildImage(selectResourceDialog.Resource)
			End If
		End Sub

		Private Sub drawRectangleToolStripButton_Click(sender As Object, e As System.EventArgs)
			If SetPenSettings() Then
				_currentFigureButton = drawRectangleToolStripButton
				_editorTool.StartBuildRectangle(_pen, Nothing)
			End If
		End Sub

		Private Sub fillRectangleToolStripButton_Click(sender As Object, e As EventArgs)
			If SetBrushSettings() Then
				_currentFigureButton = fillRectangleToolStripButton
				_editorTool.StartBuildRectangle(Nothing, _brush)
			End If
		End Sub

		Private Sub fillRectangleUseBlendingModeToolStripButton_Click(sender As Object, e As EventArgs)
			If SetHighlightBrushSettings() Then
				_currentFigureButton = fillRectangleUseBlendingModeToolStripButton
				_editorTool.StartBuildRectangle(Nothing, _brush)
			End If
		End Sub

		Private Sub drawLinesUseBlendingModeToolStripButton_Click(sender As Object, e As EventArgs)
			If SetHighlightPenSettings() Then
				_currentFigureButton = drawLinesUseBlendingModeToolStripButton
				_editorTool.StartBuildFreehandLines(_pen)
			End If
		End Sub

		Private Sub drawEllipseToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() AndAlso SetBrushSettings() Then
				_currentFigureButton = drawEllipseToolStripButton
				_editorTool.StartBuildEllipse(_pen, _brush)
			End If
		End Sub

		Private Sub drawLinesToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() Then
				_currentFigureButton = drawLinesToolStripButton
				_editorTool.StartBuildLines(_pen)
			End If
		End Sub

		Private Sub freeHandLineToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() Then
				_currentFigureButton = freeHandLineToolStripButton
				_editorTool.StartBuildFreehandLines(_pen)
			End If
		End Sub

		Private Sub drawCurvesToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() Then
				_currentFigureButton = drawCurvesToolStripButton
				_editorTool.StartBuildCurves(_pen)
			End If
		End Sub

		Private Sub drawPolygonToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() AndAlso SetBrushSettings() Then
				_currentFigureButton = drawPolygonToolStripButton
				_editorTool.StartBuildPolygon(_pen, _brush)
			End If
		End Sub

		Private Sub drawClosedCurvesToolStripButton_Click(sender As Object, e As EventArgs)
			If SetPenSettings() AndAlso SetBrushSettings() Then
				_currentFigureButton = drawClosedCurvesToolStripButton
				_editorTool.StartBuildClosedCurves(_pen, _brush)
			End If
		End Sub

		Private Sub _editorTool_FinishBuildFigure(sender As Object, e As EventArgs)
			If _currentFigureButton IsNot Nothing Then
				_currentFigureButton.Checked = False
			End If
			_currentFigureButton = Nothing
		End Sub

		Private Sub _editorTool_BeginBuildFigure(sender As Object, e As EventArgs)
			If _currentFigureButton IsNot Nothing Then
				_currentFigureButton.Checked = True
			End If
		End Sub


		''' <summary>
		''' Save the start time of the action.
		''' </summary>
		Private Sub StartAction(actionName As String)
		End Sub

		''' <summary>
		''' Shows the time of the action.
		''' </summary>
		Private Sub EndAction()
		End Sub

		#End Region

	End Class
End Namespace
